/**
 * // Copyright (c) 2023 KylinOS. All rights reserved.
 * //
 * // This program is free software; you can redistribute it
 * // and/or modify it under the terms of the GNU General Public
 * // License (version 2) as published by the FSF - Free Software
 * // Foundation*
 */

/**
 * // Copyright (C) 2023 en LG <guoliben@kylinos.cn>
 */

package docextractor

import (
	"io/ioutil"
	"os"
	"regexp"
	"time"
	"unsafe"

	"github.com/golovers/xtract"

	"gitee.com/guoliben/kydlp-doctotext/extractor_interface"
	"gitee.com/guoliben/kydlp-doctotext/utils/ctrlledprint"
)

type DocHtml struct{}

// 解析 html 文档内容
func (extractor DocHtml) GetTextFromFile(path string, size int64, filetype string, depth int) ([]byte, string) {
	ctrlledprint.Println("提取文本: 正在解析html(file)文档...")

	return DocText{}.GetTextFromFile(path, size, filetype, depth)

	start := time.Now()

	file, err := os.Open(path)
	if err != nil {
		ctrlledprint.Println("Error opening html file:", err)
		return nil, ""
	}
	defer file.Close()

	content, err := ioutil.ReadAll(file)
	if err != nil {
		ctrlledprint.Println("Error reading html file:", err)
		return nil, ""
	}

	re := regexp.MustCompile(`<script[\s\S]*?</script>`)
	output := re.ReplaceAll(content, []byte("<script></script>"))

	text := xtract.Value(*(*string)(unsafe.Pointer(&output)))

	elapsed := time.Since(start)

	return []byte(text), elapsed.String()

}

func (extractor DocHtml) GetTextFromBuffer(path string, data []byte, size int64, filetype string, depth int) ([]byte, string) {
	ctrlledprint.Println("提取文本: 正在解析html(buffer)文档...")

	return DocText{}.GetTextFromBuffer(path, data, size, filetype, depth)

	start := time.Now()

	re := regexp.MustCompile(`<script[\s\S]*?</script>`)
	output := re.ReplaceAll(data, []byte("<script></script>"))

	text := xtract.Value(*(*string)(unsafe.Pointer(&output)))

	elapsed := time.Since(start)

	return []byte(text), elapsed.String()
}

func (extractor DocHtml) GetStringTextFromFile(path string, size int64, filetype string, depth int) (string, string) {
	ctrlledprint.Println("提取文本: 正在解析html(file)文档...")

	return DocText{}.GetStringTextFromFile(path, size, filetype, depth)

	start := time.Now()

	file, err := os.Open(path)
	if err != nil {
		ctrlledprint.Println("Error opening html file:", err)
		return "", ""
	}
	defer file.Close()

	content, err := ioutil.ReadAll(file)
	if err != nil {
		ctrlledprint.Println("Error reading html file:", err)
		return "", ""
	}

	re := regexp.MustCompile(`<script[\s\S]*?</script>`)
	output := re.ReplaceAll(content, []byte("<script></script>"))

	text := xtract.Value(*(*string)(unsafe.Pointer(&output)))

	elapsed := time.Since(start)

	return text, elapsed.String()

}

func (extractor DocHtml) GetStringTextFromBuffer(path string, data []byte, size int64, filetype string, depth int) (string, string) {
	ctrlledprint.Println("提取文本: 正在解析html(buffer)文档...")

	return DocText{}.GetStringTextFromBuffer(path, data, size, filetype, depth)

	start := time.Now()

	re := regexp.MustCompile(`<script[\s\S]*?</script>`)
	output := re.ReplaceAll(data, []byte("<script></script>"))

	text := xtract.Value(*(*string)(unsafe.Pointer(&output)))

	elapsed := time.Since(start)

	return text, elapsed.String()
}

//func (extractor DocHtml) GetTextFromFileWithCallback(path string, size int64, filetype string, depth int, callback extractor_interface.OnTextDetectHandler, context interface{}) (int, string, string) {
//	var retcode int
//
//	text, elapsed := extractor.GetTextFromFile(path, size, filetype, depth)
//
//	if callback != nil {
//		retcode, _ = callback(path, size, filetype, text, len(text), context)
//	}
//
//	return retcode, text, elapsed
//}
//
//func (extractor DocHtml) GetTextFromBufferWithCallback(path string, data []byte, size int64, filetype string, depth int, callback extractor_interface.OnTextDetectHandler, context interface{}) (int, string, string) {
//	var retcode int
//
//	text, elapsed := extractor.GetTextFromBuffer(path, data, size, filetype, depth)
//
//	if callback != nil {
//		retcode, _ = callback(path, size, filetype, text, len(text), context)
//	}
//
//	return retcode, text, elapsed
//}

func (extractor DocHtml) GetTextFromFileWithCallback(path string, size int64, filetype string, depth int, callback extractor_interface.OnTextDetectHandler, context interface{}) (int, []byte, string) {
	return DocText{}.GetTextFromFileWithCallback(path, size, filetype, depth, callback, context)
}

func (extractor DocHtml) GetTextFromBufferWithCallback(path string, data []byte, size int64, filetype string, depth int, callback extractor_interface.OnTextDetectHandler, context interface{}) (int, []byte, string) {
	return DocText{}.GetTextFromBufferWithCallback(path, data, size, filetype, depth, callback, context)
}

func (extractor DocHtml) GetStringTextFromFileWithCallback(path string, size int64, filetype string, depth int, callback extractor_interface.OnStringTextDetectHandler, context interface{}) (int, string, string) {
	return DocText{}.GetStringTextFromFileWithCallback(path, size, filetype, depth, callback, context)
}

func (extractor DocHtml) GetStringTextFromBufferWithCallback(path string, data []byte, size int64, filetype string, depth int, callback extractor_interface.OnStringTextDetectHandler, context interface{}) (int, string, string) {
	return DocText{}.GetStringTextFromBufferWithCallback(path, data, size, filetype, depth, callback, context)
}
